{header}
{save_card}
<form>
  <div class="form-group">
    <label for="requirement">文案要求</label>
    {requirement_item}
  </div>
  <div class="form-group">
    <label>领域</label>
    {type_buttons}
  </div>
  <div class="form-group">
    <label>选题</label>
    <div class="list-group" id="topic_list" style="max-height: 300px;overflow-y: auto;">
	  {topic_buttons}
    </div>
  </div>
  <script type="text/javascript">
    var current_type = {topic_type_id};
    var current_type_str = '{topic_type}';
    var current_topic = {topic_id};
    var is_check = 0;
    function active_type(type, type_str) {
      $('#type' + current_type).removeClass('active');
      current_type = type;
	  current_type_str = type_str;
      $('#type' + type).addClass('active');
      $.ajax({
        url: '{base_url}library/get_copywrite_topic',
        type: 'post',
        data: {
          topic_type: type_str
        },
        success: function (json) {
          $('#topic_list').empty();
          for (var i in json) {
            var v = json[i];
            if (current_topic === v['id']) {
              $('#topic_list').append('<button type="button" class="list-group-item list-group-item-action active" id="topic' + v['id'] + '" onclick="active_topic(' + v['id'] + ')">' + v['movie_name'] + '</button>');
            } else {
              $('#topic_list').append('<button type="button" class="list-group-item list-group-item-action" id="topic' + v['id'] + '" onclick="active_topic(' + v['id'] + ')">' + v['movie_name'] + '</button>');
            }
          }
        }
      });
    }
    function active_topic(id) {
      $('#topic' + current_topic).removeClass('active');
      current_topic = id;
      $('#topic' + id).addClass('active');
    }
    function check_user() {
      $.ajax({
        url: '{base_url}library/check_user.php',
        type: 'post',
        data: {
          id: $('#user').val()
        },
        success: function (data) {
          var d = data.split(':');
          if (d.length !== 2) {
            console.error(data);
            layer.alert('服务器返回非法数据', {icon: 2});
            return;
          }
          if (d[0] === 'fail') {
            layer.alert('检查失败:' + d[1], {icon: 2});
            console.error('检查失败:' + d[1]);
          } else if (d[0] === 'success') {
            if (parseInt(d[1]) > 0) {
              is_check = 1;
              layer.alert('该账号存在');
            } else {
              is_check = 0;
              layer.alert('该账号不存在');
            }
          } else {
            console.error(data);
            layer.alert('服务器返回多余数据', {icon: 2});
          }
        }
      });
    }
    $(function () {
      $('#save').on('click', function () {
        var user = '';
	if ($('#user').length > 0) {
	  user = $('#user').val();
          if (user !== '' && is_check === 0) {
	    layer.alert('请先点击“检查”确保账号存在', {icon: 0});
            return;
	  }
	}
        var requirement = $('#requirement').val();
        if (requirement === '') {
          layer.alert('表单不能有空', {icon: 0});
          return;
        } else if (requirement.indexOf('\'') !== -1 || requirement.indexOf('"') !== -1) {
          layer.alert('表单不能含有引号', {icon: 0});
          return;
        } else if (current_topic === 0 || current_type === '') {
          layer.alert('必须选择领域与选题', {icon: 0});
          return;
        }
        var save = $('#save');
        save.attr('disabled', true);
        $.ajax({
          url: '{base_url}library/update_copywrite',
          type: 'post',
          data: {
            id: {id},
			is_check: is_check,
            requirement: requirement,
            topic_id: current_topic,
            topic_type: current_type
          },
          success: function (data) {
            var d = data.split(':');
            save.attr('disabled', false);
            if (d.length !== 2) {
              console.error(data);
              layer.alert('服务器返回非法数据', {icon: 2});
              return;
            }
            if (d[0] === 'fail') {
              layer.alert('上传失败:' + d[1], {icon: 2});
              console.error('上传失败:' + d[1]);
            } else if (d[0] === 'success') {
              if (ws !== null && ws.readyState === 1) {
                ws.send('receive_id"="0";"msg"="msg"-"copywrite_list');
                ws.send('group_topic_id_timer"="' + d[1]);
              }
              location.href = '{base_url}copywrite_list.php';
            } else {
              console.error(data);
              layer.alert('服务器返回多余数据', {icon: 2});
            }
          },
          error: function (data) {
            layer.alert('上传失败', {icon: 2});
            console.error(data.responseText);
          }
        });
      });
    });
  </script>
</form>
{footer}

